<template>
  <div id="app">
    <keep-alive include="Index">
      <router-view v-if = "isRouterAlive"></router-view>
    </keep-alive>
  </div>
</template>

<script>
  export default {
    name: 'App',
    provide(){
      return{
        reload: this.reload
      }
    },
    components: {
    },
    data() {
      return {
        isRouterAlive: true,
      }
    },
    created() {

      // 在页面加载时读取localStorage
      if (localStorage.getItem('store')) {
        this.$store.replaceState(Object.assign({}, this.$store.getters.getState, JSON.parse(localStorage.getItem('store'))))
      }
      // 在页面刷新之前将store保存到localStorage里
      window.addEventListener('beforeunload', () => {
        localStorage.setItem('store', JSON.stringify(this.$store.getters.getState))
      })
    },
    methods: {
      reload (){
        this.isRouterAlive = false
        this.$nextTick(function(){
          this.isRouterAlive = true
        })
      }
    }
  }

</script>

<style>
  @import "./assets/css/base.css";

  .nav-bar-cpn {
    position: absolute;

    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
</style>
